package com.dlmz.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.dlmz.util.BaseDao;
import com.dlmz.util.ExcelExporter;

@SuppressWarnings("serial")
public class MedicalchartManage extends JFrame implements ActionListener{
	private JLabel queryLab;  
    private JTextField queryTxt;  
    private JTable resultTb;  
    private JScrollPane jsp;  
    private JButton addBtn,deleteBtn,updateBtn,allBtn,queryBtn,count;  
    private JPanel top,bottom;   
    //========  
    private MedicalModel mm;  
    String sql = "SELECT mnum,b.`Sno`,a.`Sname`,Dname,Mdate,Diagnosis,Mrecord "
    		+ "FROM student AS a,medicalchart AS b WHERE a.`Sno`=b.`Sno` ORDER BY mnum"; 
	public MedicalchartManage() {
		// TODO Auto-generated constructor stub
		super("病历管理系统");
        /***************************初始化面板控件***********************/  
        //========查询栏  
        queryLab = new JLabel("请输入病历号或姓名或学号:");  
        queryTxt = new JTextField(10);  
        queryBtn = new JButton("查询");  
        //......添加查询栏监听  
        queryBtn.addActionListener(this);  
        queryBtn.setActionCommand("query");  //确定使用的组件 
        //========增删改统计栏  
        addBtn = new JButton("添加");  
        deleteBtn = new JButton("删除");  
        updateBtn = new JButton("修改"); 
        count = new JButton("打印");
        //......添加增删改统计栏监听  
        addBtn.addActionListener(this);  
        addBtn.setActionCommand("add");  
        deleteBtn.addActionListener(this);  
        deleteBtn.setActionCommand("delete");  
        updateBtn.addActionListener(this);  
        updateBtn.setActionCommand("update");  
        count.addActionListener(this);
        count.setActionCommand("count"); 
  
        //========查询栏  
        top = new JPanel();  
        top.add(queryLab);  
        top.add(queryTxt);  
        top.add(queryBtn);   
        //......底层增删改统计栏  
        bottom = new JPanel();  
        bottom.add(addBtn);  
        bottom.add(deleteBtn);  
        bottom.add(updateBtn); 
        bottom.add(count);
        //......中间层显示栏  
        mm = new MedicalModel();  
        mm.queryMedical(sql);  
        resultTb = new JTable(mm);  
        jsp = new JScrollPane(resultTb);  
        //......构建整体布局  
        this.add(top,BorderLayout.NORTH);  
        this.add(jsp,BorderLayout.CENTER);  
        this.add(bottom,BorderLayout.SOUTH);  
        //========设置窗口属性  
        validate();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(1000,400);
		this.setLocationRelativeTo(null);
		setVisible(true);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (JOptionPane.showConfirmDialog(MedicalchartManage.this, "是否退出病历系统？")==JOptionPane.OK_OPTION) {
					dispose();
					new MenuJframe();
				}else {
					new MedicalchartManage();
				}
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		  
        if(e.getActionCommand().equals("query")) {  
            /*********************查询***********************/  
            //========获取输入学生的姓名  
            String name = queryTxt.getText().trim();  //trim()去除字符串的头尾空格
            String id = queryTxt.getText().trim();
            String mnum = queryTxt.getText().trim();
            if(name.length() != 0 ||id.length() != 0) {  
                //========姓名输入有效时，执行查询  
                //......定义参数  
                String sql2 = "SELECT * FROM ("+sql+") AS c WHERE Mnum=? OR Sno=? OR Sname =?"; 

                //......更新模型  
                jtableUpdate(sql2,mnum,id,name);  
            } else {  
                //========为空时，设置提醒  
                JOptionPane.showMessageDialog(this, "输入不能为空");  
            }  
        } else if(e.getActionCommand().equals("add")) {  
            /*********************添加***********************/  
            new MedicalAddDialog(this, "添加病历信息", true);  
            jtableUpdate(sql, null);   
        }else if(e.getActionCommand().equals("count")) {
        	/*********************打印病历信息***********************/ 	
        	try {
    			ExcelExporter exp = new ExcelExporter();
    			exp.exportTable(resultTb, new File("病历信息表.txt"));
    			}catch (IOException ex) {
    				 System.out.println(ex.getMessage());
    				 ex.printStackTrace();
    			}
        }
        else if(e.getActionCommand().equals("delete")) {  
            /*********************删除***********************/  
            //========获取选择行号  
            int rowNum = this.resultTb.getSelectedRow();  
            if(rowNum == -1) {  
                JOptionPane.showMessageDialog(this, "请选择一行");  
                return ;  
            }  
            //========获取学生学号  
            String mnum = (String) mm.getValueAt(rowNum, 0); 
            BaseDao basedao=new BaseDao();
            //========删除学生  
            String sql3 = "delete from Medicalchart where Mnum=?";  
            int num=basedao.update(sql3, mnum);
			if(num>0) {
				JOptionPane.showMessageDialog(null, "删除病历信息成功！");
			}else {
				JOptionPane.showMessageDialog(null, "删除病历信息失败！");
			}     
            //========更新模型  
            jtableUpdate(sql, null);  
        } else if(e.getActionCommand().equals("update")) {  
            /*********************修改***********************/  
            //========获取选择行号  
            int rowNum = this.resultTb.getSelectedRow();  
            if(rowNum == -1) {  
                JOptionPane.showMessageDialog(this, "请选择一行");  
                return ;  
            }  
            new MedicalUpdateDialog(this, "修改病历信息", true, mm, rowNum);  
            jtableUpdate(sql, null); 
        }  
    }  
      
    //========更新JTable内数据  
    public void jtableUpdate(String sql,Object...objects)  
    {  
        //......创建模型  
        mm = new MedicalModel();  
        mm.queryMedical(sql,objects);  
        //......更新显示  
        resultTb.setModel(mm);  
    }  
    public static void main(String args[]) {
    	new MedicalchartManage();
    }

}
